***********************************************************************************
***********************************************************************************
*																			      *
* 	Replication file for "How do voters form perceptions of party positions?"     *
*																			      *
* 	by Markus Wagner & Thomas M. Meyer                                            *
*																			      *
***********************************************************************************
***********************************************************************************


*** packages
net install gr0070.pkg, from(http://www.stata-journal.com/software/sj17-3)
net install st0243_1.pkg, from(http://www.stata-journal.com/software/sj12-2/)
net install st0085_2.pkg, from(http://www.stata-journal.com/software/sj14-2/)
set scheme plotplain


*** Set working directories	
global DATA "SETYOURWORKDIR" // (e.g. "C:/Users/username/Replication") 
global GRAPHS "SETYOURWORKDIR"



***************************************
*** Study I: economic issues
***************************************
	
*** Load data
cd "$DATA"
use study_I.dta, clear


*** Figures & Tables

*** Figure 1
hist taxes,width(1) title("Taxes") xlabel(0 `" "More taxes" "& charges" "' 10 `" "Less taxes" "& charges" "') xscale(range(1 11)) xtitle("") percent yscale(range(0 30))
cd "$GRAPHS"
graph save taxes.gph, replace

hist pensions,width(1) title("Pensions") xlabel(0 `" "More benefits" "for the elderly" "' 10 `" "Less benefits" "for the elderly" "') xscale(range(1 11)) xtitle("") percent yscale(range(0 30))
cd "$GRAPHS"
graph save pensions.gph, replace

graph combine pensions.gph taxes.gph , col(2) xsize(8) ysize(4) ycommon
graph export "$GRAPHS/fig1.tif", replace

erase taxes.gph
erase pensions.gph



*** Figure 3: Plot mean perceived positions
tabstat  econ_lr, by(treat) stats(mean sd semean n)
* => perceived position varies from 4.0 to 4.8 (0.8 scale points)

regress econ_lr ib3.treat
margins, at(treat=(1 2 3 4))
marginsplot, horizontal  xlabel(3.5(0.5)5.25) ///
	xtitle("Mean perceived position") title("") ytitle("Text length") ///
	recast(scatter) ///
	recastci(rspike)
cd "$GRAPHS"
graph export "$GRAPHS/fig3.tif", replace



*** Figure 5: A causal mediation model to distinguish
* - direct effect of issue emphasis (treatment) FROM
* - indirect effect via differences in perceived positions

* note: delta & zeta are identical for 0/1 in linear models (see https://journals.sagepub.com/doi/pdf/10.1177/1536867X1201100407; p. 614, fn 8) 

tab treat, gen(treatdum)
gen point_m1=.
gen upper_m1=.
gen lower_m1=.

gen point_m2=.
gen upper_m2=.
gen lower_m2=.

gen point_m3=.
gen upper_m3=.
gen lower_m3=.


gen x_var=_n in 1/3


label define mediation_treat 1 "Taxes (R) > Pensions (L)"  2 "Both long"  3 "Pensions (L) > Taxes (R)" 							 
							 
label values x_var mediation_treat



* Taxes (R) > Pensions (L)  vs both short
medeff (regress pensions treatdum1 treatdum2 treatdum4  ) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes), mediate(pensions) treat(treatdum1) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==1 // direct effect 
replace point_m2=r(delta0) if x_var==1 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==1 // direct effect 
replace lower_m2=r(delta0lo) if x_var==1 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==1 // direct effect 
replace upper_m2=r(delta0hi) if x_var==1 // average causal median effect (via position)


* both long vs both short
medeff (regress pensions treatdum1 treatdum2 treatdum4  ) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes), mediate(pensions) treat(treatdum2) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==2 // direct effect 
replace point_m2=r(delta0) if x_var==2 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==2 // direct effect 
replace lower_m2=r(delta0lo) if x_var==2 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==2 // direct effect 
replace upper_m2=r(delta0hi) if x_var==2 // average causal median effect (via position)


* Pensions (L) > Taxes (R) vs both short
medeff (regress pensions treatdum1 treatdum2 treatdum4  ) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes), mediate(pensions) treat(treatdum4) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==3 // direct effect 
replace point_m2=r(delta0) if x_var==3 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==3 // direct effect 
replace lower_m2=r(delta0lo) if x_var==3 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==3 // direct effect 
replace upper_m2=r(delta0hi) if x_var==3 // average causal median effect (via position)



scatter x_var point_m1  in 1/3 || rspike upper_m1 lower_m1 x_var, horizontal  xlabel(-1(0.5)1) ylabel(1(1)3, valuelabel notick labsize(medsmall)) yscale(range(0.75 3.25) noline ) ysize(4) xsize(6)  aspectratio(0.9) ///
	xtitle("Change in perceived economic position") title("Direct effect: Emphasis change") ytitle(" ") legend(off) xline(0)  
cd "$GRAPHS"
graph save direct_economy.gph, replace

scatter x_var point_m2  in 1/3 || rspike upper_m2 lower_m2 x_var, horizontal  xlabel(-1(0.5)1) ylabel(, nolabel notick) yscale(range(0.75 3.25) noline) ysize(4) xsize(4)  aspectratio(0.9)   ///
	xtitle("Change in perceived economic position") title("Mediated effect: Position on Pensions") ytitle(" ") legend(off) xline(0)   fxsize(100) fysize(100)

cd "$GRAPHS"
graph save mediator_pensions.gph, replace



* Taxes (R) > Pensions (L)  vs both short
medeff (regress taxes treatdum1 treatdum2 treatdum4 ) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes), mediate(taxes) treat(treatdum1) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==1 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==1 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==1 // average causal median effect (via position)



* both long vs both short
medeff (regress taxes treatdum1 treatdum2 treatdum4 ) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes), mediate(taxes) treat(treatdum2) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==2 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==2 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==2 // average causal median effect (via position)



* Pensions (L) > Taxes (R) vs both short
medeff (regress taxes treatdum1 treatdum2 treatdum4 ) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes), mediate(taxes) treat(treatdum4) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==3 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==3 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==3 // average causal median effect (via position)


scatter x_var point_m3  in 1/3 || rspike upper_m3 lower_m3 x_var, horizontal  xlabel(-1(0.5)1) ylabel(, nolabel notick) yscale(range(0.75 3.25) noline) ysize(4) xsize(4)  aspectratio(0.9) ///
	xtitle("Change in perceived economic position") title("Mediated effect: Position on Taxes") ytitle(" ") legend(off) xline(0)  fxsize(100) 

cd "$GRAPHS"
graph save mediator_taxes.gph, replace

graph combine direct_economy.gph mediator_pensions.gph mediator_taxes.gph , col(3) graphregion(color(white)) imargin(zero)  ycommon xcommon ysize(4) xsize(14)
graph export "$GRAPHS/fig5.tif", replace


erase mediator_taxes.gph 
erase mediator_pensions.gph
erase direct_economy.gph

drop treatdum* point* upper* lower* x_var 
label drop mediation_treat




***************************************
*** Appendix C: Balance tests

mlogit treat age 
est store balance_age

mlogit treat female 
est store balance_gender

mlogit treat i.education 
est store balance_education

mlogit treat i.partisan
est store balance_partisan


*** Table C.1: Balance tests (Study I)
cd "$GRAPHS"
esttab balance_age balance_gender balance_education balance_partisan  using "table_c1_balance_study1.rtf", replace se(2)  ///
	nonotes nonumbers nogap compress eqlabels(none) ///
	star(* 0.1 ** 0.05 *** 0.01) nobaselevels ///
	mtitles("Age"  "Sex" "Education" "Partisan (0/1)" "Partisan: which party") ///
	title("Balance tests") ///
	coeflabels(age "Age" female "Female" 2.education "Education: medium" 3.education "Education: high" 1.partisan "Close to party (0/1)"  ///
	_cons "Constant") ///
	scalars("N Observations"  "r2_p Pseudo R-squared" "chi2 LR Chi-Square test" "p Prob. Chi squared")  ///
	addnotes("Standard errors in parentheses." /// 
	"\sym{*} \(p<0.1\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)")

	
	

*******************************************************
*** Appendix D: Full regression models from the mediation analyses (without controls)
*******************************************************

*** Table D.1: Perceived issue positions (Study I)
gen tax_short=0
replace tax_short=1 if treat==4 | treat==3
ttest taxes, by(tax_short)
sdtest taxes, by(tax_short)
tabstat taxes, by(tax_short) statistics(n mean sd) columns(statistics) format(%8.3f)

gen pension_short=0
replace pension_short=1 if treat==1 | treat==3
ttest pensions, by(pension_short)
sdtest pensions, by(pension_short)
tabstat pensions, by(pension_short) statistics(n mean sd) columns(statistics) format(%8.3f)

gen tax_short_econ_lr=0 if treat==1
replace tax_short_econ_lr=1 if treat==3
gen pensions_short_econ_lr=0 if treat==4
replace pensions_short_econ_lr=1 if treat==3

ttest  econ_lr, by(tax_short_econ_lr)
sdtest econ_lr, by(tax_short_econ_lr)
tabstat econ_lr, by(tax_short_econ_lr) statistics(n mean sd) columns(statistics) format(%8.3f)
ttest econ_lr, by(pensions_short_econ_lr)
sdtest econ_lr, by(pensions_short_econ_lr)
tabstat econ_lr, by(pensions_short_econ_lr) statistics(n mean sd) columns(statistics) format(%8.3f)




*** Table D.3: Perceived positions (Study I)
regress econ_lr ib3.treat pensions taxes 
est store med_1

regress pensions ib3.treat  
est store med_2

regress taxes  ib3.treat  
est store med_3


cd "$GRAPHS"
esttab med_1     med_2      med_3    using "table_d3_mediation_regression_study1.rtf", replace se(2)  ///
	nonotes nonumbers nogap compress eqlabels(none) ///
	star(* 0.1 ** 0.05 *** 0.01) nobaselevels ///
	mtitles("Socio-cultural position"  "Pensions" "Taxes") ///
	title("Perceived positions: Regression results") ///
	coeflabels(1.treat "Taxes (R) > Pensions (L)"  2.treat "Both long" 4.treat "Pensions (L) > Taxes (R)"  ///	
	pensions "Pensions" taxes "Taxes" ///
	_cons "Constant") ///
	scalars("N Observations"  "r2_a Adjusted R squared" )  ///
	addnotes("Standard errors in parentheses." /// 
	"\sym{*} \(p<0.1\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)")

	
	
*******************************************************
*** Appendix E: Causal mediation model with controls
*******************************************************


*** Figure E.1: Direct and indirect effects of text length on the perceived party position on economic issues (accounting for age, gender, education and partisanship)
tab treat, gen(treatdum)
tab education, gen(education)
gen point_m1=.
gen upper_m1=.
gen lower_m1=.

gen point_m2=.
gen upper_m2=.
gen lower_m2=.

gen point_m3=.
gen upper_m3=.
gen lower_m3=.


gen x_var=_n in 1/3


label define mediation_treat 1 "Taxes (R) > Pensions (L)"  2 "Both long"  3 "Pensions (L) > Taxes (R)" 							 
							 
label values x_var mediation_treat


* Taxes (R) > Pensions (L)  vs both short
medeff (regress pensions treatdum1 treatdum2 treatdum4  age female education2 education3 partisan) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes age female education2 education3 partisan), mediate(pensions) treat(treatdum1) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==1 // direct effect 
replace point_m2=r(delta0) if x_var==1 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==1 // direct effect 
replace lower_m2=r(delta0lo) if x_var==1 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==1 // direct effect 
replace upper_m2=r(delta0hi) if x_var==1 // average causal median effect (via position)


* both long vs both short
medeff (regress pensions treatdum1 treatdum2 treatdum4  age female education2 education3 partisan) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes age female education2 education3 partisan), mediate(pensions) treat(treatdum2) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==2 // direct effect 
replace point_m2=r(delta0) if x_var==2 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==2 // direct effect 
replace lower_m2=r(delta0lo) if x_var==2 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==2 // direct effect 
replace upper_m2=r(delta0hi) if x_var==2 // average causal median effect (via position)


* Pensions (L) > Taxes (R) vs both short
medeff (regress pensions treatdum1 treatdum2 treatdum4 age female education2 education3 partisan ) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes age female education2 education3 partisan), mediate(pensions) treat(treatdum4) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==3 // direct effect 
replace point_m2=r(delta0) if x_var==3 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==3 // direct effect 
replace lower_m2=r(delta0lo) if x_var==3 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==3 // direct effect 
replace upper_m2=r(delta0hi) if x_var==3 // average causal median effect (via position)



scatter x_var point_m1  in 1/3 || rspike upper_m1 lower_m1 x_var, horizontal  xlabel(-1(0.5)1) ylabel(1(1)3, valuelabel labsize(medsmall) notick) yscale(range(0.75 3.25) noline ) ysize(4) xsize(6)  aspectratio(0.9) ///
	xtitle("Change in perceived economic position") title("Direct effect: Emphasis change") ytitle(" ") legend(off) xline(0)  
cd "$GRAPHS"
graph save direct_economy_control.gph, replace

scatter x_var point_m2  in 1/3 || rspike upper_m2 lower_m2 x_var, horizontal  xlabel(-1(0.5)1) ylabel(, nolabel notick) yscale(range(0.75 3.25) noline) ysize(4) xsize(4)  aspectratio(0.9)   ///
	xtitle("Change in perceived economic position") title("Mediated effect: Position on Pensions") ytitle(" ") legend(off) xline(0)   fxsize(100) fysize(100)

cd "$GRAPHS"
graph save mediator_pensions_control.gph, replace




* Taxes (R) > Pensions (L)  vs both short
medeff (regress taxes treatdum1 treatdum2 treatdum4 age female education2 education3 partisan) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes age female education2 education3 partisan), mediate(taxes) treat(treatdum1) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==1 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==1 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==1 // average causal median effect (via position)



* both long vs both short
medeff (regress taxes treatdum1 treatdum2 treatdum4 age female education2 education3 partisan) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes age female education2 education3 partisan), mediate(taxes) treat(treatdum2) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==2 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==2 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==2 // average causal median effect (via position)



* Pensions (L) > Taxes (R) vs both short
medeff (regress taxes treatdum1 treatdum2 treatdum4 age female education2 education3 partisan) (regress econ_lr treatdum1 treatdum2 treatdum4 pensions taxes age female education2 education3 partisan), mediate(taxes) treat(treatdum4) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==3 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==3 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==3 // average causal median effect (via position)


scatter x_var point_m3  in 1/3 || rspike upper_m3 lower_m3 x_var, horizontal  xlabel(-1(0.5)1) ylabel(, nolabel notick) yscale(range(0.75 3.25) noline) ysize(4) xsize(4)  aspectratio(0.9) ///
	xtitle("Change in perceived economic position") title("Mediated effect: Position on Taxes") ytitle(" ") legend(off) xline(0)  fxsize(100) 

cd "$GRAPHS"
graph save mediator_taxes_control.gph, replace

graph combine direct_economy_control.gph mediator_pensions_control.gph mediator_taxes_control.gph , col(3) graphregion(color(white)) imargin(zero)  ycommon xcommon ysize(4) xsize(14)
graph export "$GRAPHS/fig_e1.tif", replace


erase direct_economy_control.gph
erase mediator_taxes_control.gph 
erase mediator_pensions_control.gph

drop treatdum* point* upper* lower* x_var 
label drop mediation_treat



*** Table E.1: Mediation analyses including control variables - Perceived positions (Study I) incl. control variables
regress econ_lr ib3.treat pensions taxes age female ib1.education  partisan
est store med_1_control

regress pensions ib3.treat  age female ib1.education partisan
est store med_2_control

regress taxes  ib3.treat   age female ib1.education partisan
est store med_3_control

	
cd "$GRAPHS"
esttab   med_1_control      med_2_control    med_3_control   using "table_e1_mediation_regression_study1_control.rtf", replace se(2)  ///
	nonotes nonumbers nogap compress eqlabels(none) ///
	star(* 0.1 ** 0.05 *** 0.01) nobaselevels ///
	mtitles("Socio-cultural position"   "Pensions" "Taxes" ) ///
	title("Perceived positions: Regression results including control variables") ///
	coeflabels(1.treat "Taxes (R) > Pensions (L)"  2.treat "Both long" 4.treat "Pensions (L) > Taxes (R)" ///	
	age "Age" female "Female" 2.education "Education: medium" 3.education "Education: high" partisan "Close to party (0/1)"  ///
	pensions "Pensions" taxes "Taxes" ///
	_cons "Constant") ///
	scalars("N Observations"  "r2_a Adjusted R squared" )  ///
	addnotes("Standard errors in parentheses." /// 
	"\sym{*} \(p<0.1\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)")


	
	
	


****************************************************************************************************************
*** Study II: Socio-cultural issues
****************************************************************************************************************

cd "$DATA"
use "study_II.dta", clear


*** Figures & Tables

*** Figure 2
hist restrict_migration, width(1) title("Refugees") xlabel(0 `" "Open" "migration policy" "' 10 `" "Restrictive" "migration policy" "') xscale(range(1 11)) xtitle("") percent
cd "$GRAPHS"
graph save migration.gph, replace

hist lgbt_rights, width(1) title("LGBT") xlabel(0 `" "Equal rights" "for same-sex marriage" "' 10 `" "Privileges for marriages" " between men and women" "') xscale(range(1 11)) xtitle("") percent 
cd "$GRAPHS"
graph save lgbt.gph, replace


graph combine lgbt.gph migration.gph , col(2) xsize(8) ysize(4) ycommon
graph export "$GRAPHS/fig2.tif", replace

erase migration.gph
erase lgbt.gph




*** Figure 4
tabstat  prog_cons, by(treat) stats(mean sd semean n)

regress prog_cons ib3.treat
margins, at(treat=(1 2 3 4))
marginsplot, horizontal  xlabel(4.25(0.5)6.0) ///
	xtitle("Mean perceived position") title("") ytitle("Text length") ///
	recast(scatter) ///
	recastci(rspike)
cd "$GRAPHS"
graph export "$GRAPHS/fig4.tif", replace




*** Figure 6: A causal mediation model to distinguish
* - direct effect of issue emphasis (treatment) FROM
* - indirect effect via differences in perceived positions

* note: delta & zeta are identical for 0/1 in linear models (see https://journals.sagepub.com/doi/pdf/10.1177/1536867X1201100407; p. 614, fn 8) 

tab treat, gen(treatdum)
gen point_m1=.
gen upper_m1=.
gen lower_m1=.

gen point_m2=.
gen upper_m2=.
gen lower_m2=.

gen point_m3=.
gen upper_m3=.
gen lower_m3=.


gen x_var=_n in 1/3


label define mediation_treat 1 "Refugees (R) > LGBT rights (L) "  2 "Both long"  3 "LGBT rights (L) > Refugees (R)"						 							 
label values x_var mediation_treat



* Refugees (R) > LGBT (L)  vs both short
medeff (regress lgbt_rights treatdum1 treatdum2 treatdum4  ) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights), mediate(lgbt_rights) treat(treatdum1) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==1 // direct effect 
replace point_m2=r(delta0) if x_var==1 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==1 // direct effect 
replace lower_m2=r(delta0lo) if x_var==1 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==1 // direct effect 
replace upper_m2=r(delta0hi) if x_var==1 // average causal median effect (via position)


* both long vs both short
medeff (regress lgbt_rights treatdum1 treatdum2 treatdum4  ) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights), mediate(lgbt_rights) treat(treatdum2) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==2 // direct effect 
replace point_m2=r(delta0) if x_var==2 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==2 // direct effect 
replace lower_m2=r(delta0lo) if x_var==2 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==2 // direct effect 
replace upper_m2=r(delta0hi) if x_var==2 // average causal median effect (via position)


* LGBT (L) > Refugees (R) vs both short
medeff (regress lgbt_rights treatdum1 treatdum2 treatdum4  ) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights ), mediate(lgbt_rights) treat(treatdum4) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==3 // direct effect 
replace point_m2=r(delta0) if x_var==3 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==3 // direct effect 
replace lower_m2=r(delta0lo) if x_var==3 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==3 // direct effect 
replace upper_m2=r(delta0hi) if x_var==3 // average causal median effect (via position)


scatter x_var point_m1  in 1/3 || rspike upper_m1 lower_m1 x_var, horizontal  xlabel(-1(0.5)1) ylabel(1(1)3, valuelabel labsize(medsmall) notick) yscale(range(0.75 3.25) noline )  ysize(4) xsize(6) aspectratio(0.9) ///
	xtitle("Change in perceived socio-cultural position") title("Direct effect: Emphasis change") ytitle(" ") legend(off) xline(0)  
cd "$GRAPHS"
graph save direct_prog_cons.gph, replace

scatter x_var point_m2  in 1/3 || rspike upper_m2 lower_m2 x_var, horizontal  xlabel(-1(0.5)1) ylabel(, nolabel notick) yscale(range(0.75 3.25) noline) ysize(4) xsize(4)  aspectratio(0.9)   ///
	xtitle("Change in perceived socio-cultural position") title("Mediated effect: Position on LGBT rights") ytitle(" ") legend(off) xline(0)   fxsize(100) fysize(100)

cd "$GRAPHS"
graph save mediator_lgbt.gph, replace





* Refugees (R) > LGBT (L)  vs both short
medeff (regress restrict_migration treatdum1 treatdum2 treatdum4 ) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights), mediate(restrict_migration) treat(treatdum1) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==1 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==1 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==1 // average causal median effect (via position)



* both long vs both short
medeff (regress restrict_migration treatdum1 treatdum2 treatdum4 ) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights), mediate(restrict_migration) treat(treatdum2) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==2 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==2 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==2 // average causal median effect (via position)


* LGBT (L) > Refugees (R) vs both short
medeff (regress restrict_migration treatdum1 treatdum2 treatdum4 ) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights), mediate(restrict_migration) treat(treatdum4) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==3 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==3 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==3 // average causal median effect (via position)



scatter x_var point_m3  in 1/3 || rspike upper_m3 lower_m3 x_var, horizontal  xlabel(-1(0.5)1) ylabel(, nolabel notick) yscale(range(0.75 3.25) noline) ysize(4) xsize(4)  aspectratio(0.9) ///
	xtitle("Change in perceived socio-cultural position") title("Mediated effect: Position on Refugees") ytitle(" ") legend(off) xline(0)  fxsize(100) 

cd "$GRAPHS"
graph save mediator_refugees.gph, replace



graph combine direct_prog_cons.gph mediator_lgbt.gph mediator_refugees.gph, col(3) graphregion(color(white)) imargin(zero)  xcommon ycommon xsize(14)
graph export "$GRAPHS/fig6.tif", replace


erase direct_prog_cons.gph
erase mediator_lgbt.gph 
erase mediator_refugees.gph

drop treatdum* point* upper* lower* x_var 
label drop mediation_treat





******************************************************
*** Appendix C: Balance tests
*******************************************************

mlogit treat age , base(2)
est store balance_age 

mlogit treat i.female , base(2)
est store balance_gender

mlogit treat i.education , base(2)
est store balance_education

mlogit treat i.partisan, base(2)
est store balance_partisan


*** Table C.2: Balance tests (Study II)
cd "$GRAPHS"
esttab balance_age balance_gender balance_education balance_partisan  using "balance_study2.rtf", replace se(2)  ///
	nonotes nonumbers nogap compress eqlabels(none) ///
	star(* 0.1 ** 0.05 *** 0.01) nobaselevels ///
	mtitles("Age"  "Sex" "Education" "Partisan (0/1)" "Partisan: which party") ///
	title("Balance tests") ///
	coeflabels(age "Age" 1.female "Female" 2.education "Education: medium" 3.education "Education: high" 1.partisan "Close to party (0/1)"  ///
	_cons "Constant") ///
	scalars("N Observations"  "r2_p Pseudo R-squared" "chi2 LR Chi-Square test" "p Prob. Chi squared")  ///
	addnotes("Standard errors in parentheses." /// 
	"\sym{*} \(p<0.1\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)")



******************************************************
*** Appendix D: Regression models from the mediation analysis
*******************************************************


*** Table D.2: Perceived issue positions (Study II)
gen ref_short=0
replace ref_short=1 if treat==4 | treat==3
ttest restrict_migration, by(ref_short)
sdtest restrict_migration, by(ref_short)
tabstat restrict_migration, by(ref_short) statistics(n mean sd) columns(statistics) format(%8.3f)

gen lgbt_short=0
replace lgbt_short=1 if treat==1 | treat==3
ttest lgbt_rights, by(lgbt_short)
sdtest lgbt_rights, by(lgbt_short)
tabstat lgbt_rights, by(lgbt_short) statistics(n mean sd) columns(statistics) format(%8.3f)


gen ref_short_prog_cons=0 if treat==1
replace ref_short_prog_cons=1 if treat==3
gen lgbt_short_prog_cons=0 if treat==4
replace lgbt_short_prog_cons=1 if treat==3

ttest  prog_cons, by(ref_short_prog_cons)
sdtest prog_cons, by(ref_short_prog_cons)
tabstat prog_cons, by(ref_short_prog_cons) statistics(n mean sd) columns(statistics) format(%8.3f)
ttest prog_cons, by(lgbt_short_prog_cons)
sdtest prog_cons, by(lgbt_short_prog_cons)
tabstat prog_cons, by(lgbt_short_prog_cons) statistics(n mean sd) columns(statistics) format(%8.3f)


** Table D.4: Perceived positions (Study II)
regress prog_cons ib3.treat restrict_migration lgbt_rights 
est store med_1

regress lgbt_rights ib3.treat  
est store med_2

regress restrict_migration  ib3.treat  
est store med_3


cd "$GRAPHS"
esttab med_1     med_2      med_3    using "table_d4_mediation_regression_study2.rtf", replace se(2)  ///
	nonotes nonumbers nogap compress eqlabels(none) ///
	star(* 0.1 ** 0.05 *** 0.01) nobaselevels ///
	mtitles("Socio-cultural position"  "LGBT rights" "Refugees") ///
	title("Perceived positions: Regression results") ///
	coeflabels(1.treat "Refugees (R) > LGBT (L)"  2.treat "Both long" 4.treat "LGBT (L) > Refugees (R)" ///	
	lgbt_rights "LGBT rights" restrict_migration "Refugees" ///
	_cons "Constant") ///
	scalars("N Observations"  "r2_a Adjusted R squared" )  ///
	addnotes("Standard errors in parentheses." /// 
	"\sym{*} \(p<0.1\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)")

	
	
	
******************************************************
*** Appendix E:  Causal mediation model with controls
*******************************************************

*** Figure E.2: Direct and indirect effects of text length on the perceived party position on socio-cultural issues
tab treat, gen(treatdum)
tab education, gen(education)

gen point_m1=.
gen upper_m1=.
gen lower_m1=.

gen point_m2=.
gen upper_m2=.
gen lower_m2=.

gen point_m3=.
gen upper_m3=.
gen lower_m3=.


gen x_var=_n in 1/3


label define mediation_treat 1 "Refugees (R) > LGBT rights (L) "  2 "Both long"  3 "LGBT rights (L) > Refugees (R)"						 							 
label values x_var mediation_treat



* Refugees (R) > LGBT (L)  vs both short
medeff (regress lgbt_rights treatdum1 treatdum2 treatdum4  age female education2 education3 partisan) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights age female education2 education3 partisan), mediate(lgbt_rights) treat(treatdum1) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==1 // direct effect 
replace point_m2=r(delta0) if x_var==1 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==1 // direct effect 
replace lower_m2=r(delta0lo) if x_var==1 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==1 // direct effect 
replace upper_m2=r(delta0hi) if x_var==1 // average causal median effect (via position)


* both long vs both short
medeff (regress lgbt_rights treatdum1 treatdum2 treatdum4  age female education2 education3 partisan) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights age female education2 education3 partisan), mediate(lgbt_rights) treat(treatdum2) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==2 // direct effect 
replace point_m2=r(delta0) if x_var==2 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==2 // direct effect 
replace lower_m2=r(delta0lo) if x_var==2 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==2 // direct effect 
replace upper_m2=r(delta0hi) if x_var==2 // average causal median effect (via position)


* LGBT (L) > Refugees (R) vs both short
medeff (regress lgbt_rights treatdum1 treatdum2 treatdum4  age female education2 education3 partisan) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights age female education2 education3 partisan), mediate(lgbt_rights) treat(treatdum4) sims(2000) seed(12345)

replace point_m1=r(zeta0) if x_var==3 // direct effect 
replace point_m2=r(delta0) if x_var==3 // average causal median effect (via position)

replace lower_m1=r(zeta0lo) if x_var==3 // direct effect 
replace lower_m2=r(delta0lo) if x_var==3 // average causal median effect (via position)

replace upper_m1=r(zeta0hi) if x_var==3 // direct effect 
replace upper_m2=r(delta0hi) if x_var==3 // average causal median effect (via position)


scatter x_var point_m1  in 1/3 || rspike upper_m1 lower_m1 x_var, horizontal  xlabel(-1(0.5)1) ylabel(1(1)3, valuelabel labsize(medsmall) notick) yscale(range(0.75 3.25) noline )  ysize(4) xsize(6) aspectratio(0.9) ///
	xtitle("Change in perceived socio-cultural position") title("Direct effect: Emphasis change") ytitle(" ") legend(off) xline(0)  
cd "$GRAPHS"
graph save direct_prog_cons_control.gph, replace

scatter x_var point_m2  in 1/3 || rspike upper_m2 lower_m2 x_var, horizontal  xlabel(-1(0.5)1) ylabel(, nolabel notick) yscale(range(0.75 3.25) noline) ysize(4) xsize(4)  aspectratio(0.9)   ///
	xtitle("Change in perceived socio-cultural position") title("Mediated effect: Position on LGBT rights") ytitle(" ") legend(off) xline(0)   fxsize(100) fysize(100)

cd "$GRAPHS"
graph save mediator_lgbt_control.gph, replace



* Refugees (R) > LGBT (L)  vs both short
medeff (regress restrict_migration treatdum1 treatdum2 treatdum4 age female education2 education3 partisan) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights age female education2 education3 partisan), mediate(restrict_migration) treat(treatdum1) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==1 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==1 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==1 // average causal median effect (via position)



* both long vs both short
medeff (regress restrict_migration treatdum1 treatdum2 treatdum4 age female education2 education3 partisan) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights age female education2 education3 partisan), mediate(restrict_migration) treat(treatdum2) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==2 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==2 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==2 // average causal median effect (via position)


* LGBT (L) > Refugees (R) vs both short
medeff (regress restrict_migration treatdum1 treatdum2 treatdum4 age female education2 education3 partisan) (regress prog_cons treatdum1 treatdum2 treatdum4 restrict_migration lgbt_rights age female education2 education3 partisan), mediate(restrict_migration) treat(treatdum4) sims(2000) seed(12345)

replace point_m3=r(delta0) if x_var==3 // average causal median effect (via position)

replace lower_m3=r(delta0lo) if x_var==3 // average causal median effect (via position)

replace upper_m3=r(delta0hi) if x_var==3 // average causal median effect (via position)


scatter x_var point_m3  in 1/3 || rspike upper_m3 lower_m3 x_var, horizontal  xlabel(-1(0.5)1) ylabel(, nolabel notick) yscale(range(0.75 3.25) noline) ysize(4) xsize(4)  aspectratio(0.9) ///
	xtitle("Change in perceived socio-cultural position") title("Mediated effect: Position on Refugees") ytitle(" ") legend(off) xline(0)  fxsize(100) 

cd "$GRAPHS"
graph save mediator_refugees_control.gph, replace



graph combine direct_prog_cons_control.gph mediator_lgbt_control.gph mediator_refugees_control.gph, col(3) graphregion(color(white)) imargin(zero)  xcommon ycommon xsize(14)
graph export "$GRAPHS/fig_e2.tif", replace



erase direct_prog_cons_control.gph
erase mediator_lgbt_control.gph 
erase mediator_refugees_control.gph

drop treatdum* point* upper* lower* x_var 
label drop mediation_treat



***  Table E.2: Perceived positions (Study II) incl. control variables
regress prog_cons ib3.treat restrict_migration lgbt_rights age female ib1.education  partisan
est store med_1_control

regress lgbt_rights ib3.treat  age female ib1.education partisan
est store med_2_control

regress restrict_migration  ib3.treat   age female ib1.education partisan
est store med_3_control



cd "$GRAPHS"
esttab   med_1_control      med_2_control    med_3_control   using "table_e2_mediation_regression_study2_control.rtf", replace se(2)  ///
	nonotes nonumbers nogap compress eqlabels(none) ///
	star(* 0.1 ** 0.05 *** 0.01) nobaselevels ///
	mtitles("Socio-cultural position"   "LGBT rights" "Refugees" ) ///
	title("Perceived positions: Regression results including control variables") ///
	coeflabels(1.treat "Refugees (R) > LGBT (L)"  2.treat "Both long" 4.treat "LGBT (L) > Refugees (R)" ///	
	age "Age" female "Female" 2.education "Education: medium" 3.education "Education: high" partisan "Close to party (0/1)"  ///
	lgbt_rights "LGBT rights" restrict_migration "Refugees" ///
	_cons "Constant") ///
	scalars("N Observations"  "r2_a Adjusted R squared" )  ///
	addnotes("Standard errors in parentheses." /// 
	"\sym{*} \(p<0.1\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)")


	
	
	
*******************************************************
*** Appendix G: (Failed) Experiment on economic issues
*******************************************************
    
*** Figure G.1: Perceived policy positions on social services (left) and deregulation (right)
tabstat  deregulation, by(treat2) stats(mean sd semean n)
tabstat  pensions, by(treat2) stats(mean sd semean n)

hist deregulation,width(1) title("Deregulation") xlabel(0 `" "Regulate" "the market" "' 10 `" "Deregulate" "the market" "') xscale(range(1 11)) xtitle("") percent
cd "$GRAPHS"
graph save deregulation.gph, replace

hist pensions,width(1) title("Pensions") xlabel(0 `" "Low taxes and" "few social services" "' 10 `" "High taxes" "and lots of social services" "') xscale(range(1 11)) xtitle("") percent
cd "$GRAPHS"
graph save taxes_services.gph, replace

graph combine taxes_services.gph deregulation.gph , col(2) xsize(8) ysize(4)
graph export "fig_g1.tif", replace

erase taxes_services.gph
erase deregulation.gph


*******************************************************
* Bibliography for packages
*******************************************************
/*
Bischof, Daniel (2017) New graphic schemes for Stata: plotplain and plottig. The Stata Journal 17(3): 748–759.

Hicks, Raymond and Tingley, Dustin (2011) Causal Mediation Analysis. The Stata Journal 11, 605-619.